package com.daiqee.service.httpclient;

import org.apache.http.conn.HttpClientConnectionManager;

/**
 * 清除无效链接请求
 * 
 * <br/>
 * @since JDK 1.8
 * @version  V2.1
 * @author RichardTang 
 * @date: 2018年5月8日 上午8:19:59
 * @package com.daiqee.service.httpclient
 * @copyright:Copyright (c) 2018, 1245811923@qq.com All Rights Reserved.
 */
public class CleanConnectionEvictor extends Thread{
	
    private final HttpClientConnectionManager connMgr;  
  
    private volatile boolean shutdown;  
  
    public CleanConnectionEvictor(HttpClientConnectionManager connMgr) {  
        this.connMgr = connMgr;  
        this.start();// 启动线程  
    }  
  
    @Override  
    public void run() {  
        try {  
            while (!shutdown) {  
                synchronized (this) {  
                    // 每隔5秒执行一个，关闭失效的http连接  
                    wait(5000);  
                    // 关闭失效的连接  
                    connMgr.closeExpiredConnections();  
                }  
            }  
        } catch (InterruptedException ex) {  
            // 结束  
        }  
    }  
  
    public void shutdown() {  
        shutdown = true;  
        synchronized (this) {  
            notifyAll();  
        }  
    }  
}
